using System;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;

namespace RaisingStudio.SmallBasicCompiler.Expressions
{
	[Serializable]
	public class PropertyExpression : Expression
	{
		public TokenInfo TypeName
		{
			get;
			set;
		}
		public TokenInfo PropertyName
		{
			get;
			set;
		}
		public override void EmitIL(CodeGenScope scope)
		{
			TypeInfo typeInfo = scope.TypeInfoBag.Types[this.TypeName.NormalizedText];
			PropertyInfo propertyInfo = typeInfo.Properties[this.PropertyName.NormalizedText];
			MethodInfo getMethod = propertyInfo.GetGetMethod();
			scope.ILGenerator.EmitCall(OpCodes.Call, getMethod, null);
		}
		public override string ToString()
		{
			return string.Format(CultureInfo.CurrentUICulture, "{0}.{1}", new object[]
			{
				this.TypeName.Text, 
				this.PropertyName.Text
			});
		}
	}
}
